<template>
  <div class="app-container machineContainer">
    <div class="customContainer">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item class="pull-right">
          <span>机台总数 {{machine_num}} 台</span>
        </el-form-item>
        <el-form-item>
          <el-select filterable placeholder="选择门店" v-model="formInline.name" @change="storeChangeF">
            <el-option
              v-for="item in store_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
          <el-select filterable placeholder="选择区域" v-model="formInline.area_id" style="width:100px">
            <el-option v-for="item in area_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
          <el-select filterable placeholder="选择类型" v-model="formInline.type_id" style="width:100px">
            <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.machine_id"
            placeholder="请输入机台ID"
            @keyup.enter.native="searchClick"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>
      <!-- 列表 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">{{scope.$index+1+(currentPage-1)*20}}</template>
        </el-table-column>
        <el-table-column align="center" label="机台ID" prop="id" width="80"></el-table-column>
        <el-table-column align="center" label="机台编号" prop="no"></el-table-column>
        <el-table-column align="center" label="类型" prop="type_name"></el-table-column>
        <el-table-column align="center" label="所属品牌" prop="brand_name"></el-table-column>
        <el-table-column align="center" label="门店" prop="store_name"></el-table-column>
        <el-table-column align="center" label="所属区域" prop="area_name"></el-table-column>
        <el-table-column align="center" label="是否支持出货">
          <template slot-scope="scope">{{scope.row.is_unitsshipped==1? '支持' : '不支持'}}</template>
        </el-table-column>
        <el-table-column align="center" label="卡头信息" width="100">
          <template slot-scope="scope">
            <router-link
              :to="{name:'information/deployclevice',params :{brand_id:scope.row.brand_id,store_id:scope.row.store_id,machine_id:scope.row.id,routerflag:1}}"
            >
              <el-button type="text" size="small">配置卡头</el-button>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column align="center" label="礼品信息" width="100">
          <template slot-scope="scope">
            <router-link
              :to="{name:'information/machine_gift_mapping',params :{machine_id:scope.row.id,routerflag:1}}"
            >
              <el-button type="text" size="small">查看礼品</el-button>
            </router-link>
          </template>
        </el-table-column>
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              @click="addEditClick(scope.row.id,scope.$index)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              @click="delClick(scope.row.id,scope.$index)"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 弹框 -->
      <el-dialog
        :close-on-click-modal="false"
        :title="dialogTitle"
        :visible.sync="addEditDialog"
        @close="callOf('saveMachineForm')"
      >
        <div v-if="dialogTitle=='编辑机台信息'" style="position:absolute;top:40px;right:30px">
          在线状态：
          <span>{{editMachineInfo.state}}</span>
        </div>
        <el-form
          class="dialogInContainer"
          v-loading="dialogLoading"
          :model="saveMachineForm"
          :inline="true"
          ref="saveMachineForm"
          :rules="rules"
          style="width:620px;margin:0 auto;"
        >
          <el-form-item label="子品牌　" prop="brand_id">
            <el-select
              v-model="saveMachineForm.brand_id"
              placeholder="子品牌"
              :disabled="isDisabled"
              @change="brandChange"
            >
              <el-option
                v-for="item in brandList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="门店" class="pull-right" prop="store_id">
            <el-select
              filterable
              v-model="saveMachineForm.store_id"
              placeholder="请选择门店"
              @change="storeChange"
              ref="storeName"
              :disabled="isDisabled"
            >
              <el-option
                v-for="item in storeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属区域" class="pull-left" prop="area_id">
            <el-select
              v-model="saveMachineForm.area_id"
              placeholder="请选择区域"
              :disabled="isDisabled"
              @change="areaChange"
            >
              <el-option
                v-for="item in areaList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="一级分类" class="pull-right" prop="oneTypeVal">
            <el-select
              filterable
              v-model="saveMachineForm.oneTypeVal"
              placeholder="请选择一级分类"
              @change="oneTypeChange"
              :disabled="isDisabled"
            >
              <el-option
                v-for="item in oneTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="二级分类" class="pull-left" prop="type_id">
            <el-select
              filterable
              v-model="saveMachineForm.type_id"
              placeholder="请选择二级分类"
              :disabled="isDisabled"
              @change="towTypeChange"
            >
              <el-option
                v-for="item in towTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机台风格" class="pull-right">
            <el-select v-model="saveMachineForm.style_id" placeholder="请选择机台风格">
              <el-option label="无" value="0"></el-option>
              <el-option
                v-for="item in styleList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机台编号" prop="no">
            <el-input :placeholder="default_no" v-model="saveMachineForm.no" class="w200"></el-input>
          </el-form-item>
          <div class="pull-right">
            <el-checkbox v-model="isSwingCard" label="是否支持刷卡" border></el-checkbox>
            <el-checkbox v-model="isDelivery" label="是否支持出货" border></el-checkbox>&nbsp;
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button
            v-if="dialogTitle!='编辑机台信息'"
            type="primary"
            @click="saveClick('saveMachineForm','addClevice')"
          >保存并添加卡头</el-button>
          <el-button type="primary" @click="saveClick('saveMachineForm')">保存</el-button>
          <el-button @click="callOf('saveMachineForm')">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 弹框 -->
      <el-dialog
        :close-on-click-modal="false"
        width="800px"
        title="添加卡头"
        :visible.sync="addCleviceDialog"
        @close="callOf('saveForm')"
      >
        <div
          style="position:relative;bottom:20px;"
        >{{machineInfo.brand_name}}&#x3000;{{machineInfo.store_name}}&#x3000;{{machineInfo.area_name}}&#x3000;{{machineInfo.machine_type}}&#x3000;机台编号：{{machineInfo.no}}&#x3000;机台ID：{{machineInfo.id}}</div>
        <el-form
          :model="saveForm"
          :inline="true"
          ref="saveForm"
          :rules="rules"
          style="width:760px;margin:0 auto;"
        >
          <div class="customHeader">
            <el-input size="mini" v-model="clevice_id" class="w200" placeholder="请输入卡头ID"></el-input>
            <el-button
              size="mini"
              type="primary"
              :disabled="!clevice_id"
              @click="searchCleviceClick"
            >搜 索</el-button>
            <div class="pull-right">
              <el-button size="mini" type="primary" @click="addCleviceClick">新建卡头</el-button>
            </div>
          </div>
          <br />
          <el-form-item label="　会员价" prop="coins_sell_vip">
            <el-input
              size="mini"
              v-model="saveForm.coins_sell_vip"
              class="w140"
              :disabled="isDisabled"
            ></el-input>
          </el-form-item>
          <el-form-item label="非会员价" prop="coins_sell">
            <el-input size="mini" v-model="saveForm.coins_sell" class="w140" :disabled="isDisabled"></el-input>
          </el-form-item>
          <el-form-item label="　脉冲数" prop="pulse">
            <el-input size="mini" v-model="saveForm.pulse" class="w140" :disabled="isDisabled"></el-input>
          </el-form-item>
          <el-form-item label="卡头类型" prop="type">
            <el-select
              class="w140"
              size="mini"
              placeholder="卡头类型"
              v-model="saveForm.type"
              filterable
            >
              <el-option
                v-for="item in clevice_type"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="· 　备注">
            <el-input size="mini" :maxlength="50" v-model="saveForm.remark" class="w140"></el-input>
          </el-form-item>
          <el-form-item class="pull-right" prop="stars" label="星星数">
            <el-input size="mini" v-model="saveForm.stars" class="w140" :disabled="isDisabled"></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox size="mini" class="w140" v-model="isGiftMapping" label="支持关联礼品" border></el-checkbox>
          </el-form-item>
        </el-form>
        <el-checkbox v-model="is_sweep" @change="sweepChange">支持扫码支付</el-checkbox>
        <br />
        <br />
        <el-checkbox :disabled="!is_sweep" v-model="is_mida" @change="midaChange">咪哒KTV</el-checkbox>&#x3000;
        <el-select
          :disabled="is_mida||!is_sweep"
          placeholder="套餐"
          size="mini"
          v-model="scanForm.combo_id"
          filterable
          @change="mealChange"
        >
          <el-option
            v-for="item in package_list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <br />
        <br />
        <el-table
          highlight-current-row
          :data="scanForm.rules"
          v-loading.body="listLoading"
          border
          size="mini"
        >
          <el-table-column align="center" label="按钮" width="80">
            <template slot-scope="scope">
              <span class="indexColor">按钮{{scope.$index+1}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="扫码金额（元）" prop="money"></el-table-column>
          <el-table-column v-if="!is_mida" align="center" label="游玩次数（次）" prop="times"></el-table-column>
          <el-table-column v-if="is_mida" align="center" label="游玩时间（分钟）" prop="times"></el-table-column>
          <el-table-column align="center" label="首单赠送" width="80" v-if="!is_mida">
            <template slot-scope="scope">
              <el-checkbox disabled v-model="scope.row.is_give"></el-checkbox>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button
            size="mini"
            type="primary"
            @click="saveCleviceClick('saveForm','keep')"
          >保存并继续添加卡头</el-button>
          <el-button size="mini" type="primary" @click="saveCleviceClick('saveForm')">添 加</el-button>
          <el-button size="mini" @click="addCleviceDialog=false">取 消</el-button>&nbsp;
        </div>
      </el-dialog>
    </div>

    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
      <div class="pull-right">
        <router-link :to="{name:'information/machineType'}">
          <el-button type="primary" size="small">机台类型管理</el-button>
        </router-link>
        <el-button type="primary" size="small" @click="addEditClick()">新增机台</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getList,
  getPage,
  del,
  getEditMachine,
  getStore,
  getArea,
  getType,
  getStyle,
  getBrand,
  save,
  getClevice,
  saveClevice,
  get_package,
  get_area_no,
} from "@/api/information/machine_list";
import { num1to255, nullOr1to255 } from "@/utils/validate";
export default {
  data() {
    const validateVal = (rule, value, callback) => {
      if (!num1to255(value)) {
        callback(new Error("请输入1-255数字"));
      } else {
        callback();
      }
    };
    const validateVal2 = (rule, value, callback) => {
      if (value && !nullOr1to255(value)) {
        callback(new Error("可不填，要填就填1-255数字"));
      } else {
        callback();
      }
    };
    return {
      default_no: "",
      store_list: [], //筛选
      area_list: [], //筛选
      type_list: [],
      package_info: { id: "" },
      package_list: [],
      mida_list: [
        { money: "15", times_first: "3", times: "3" },
        { money: "25", times_first: "5", times: "5" },
        { money: "40", times_first: "8", times: "8" },
        { money: "50", times_first: "10", times: "10" },
        { money: "75", times_first: "15", times: "15" },
      ],
      is_sweep: false,
      is_mida: false,
      scanForm: {
        id: "",
        combo_id: "",
        rules: [{ money: "", times: "", times_first: "", is_give: false }],
      },
      clevice_type: [
        { id: "0", name: "常规卡头" },
        { id: "1", name: "湖心岛门票区门票" },
        { id: "2", name: "湖心岛门票区陪同票" },
        { id: "3", name: "湖心岛门票区销售" },
      ],
      dialogLoading: false,
      isSwingCard: false,
      isDelivery: false,
      isDisabled: false,
      isGiftMapping: false,
      store_name: "",
      machine_num: "",
      dialogTitle: "",
      machineInfo: {
        brand_name: "",
        store_name: "",
        area_name: "",
        machine_type: "",
        id: "",
        no: "",
      },
      editIndex: "",
      url: "",
      list: null,
      listLoading: false,
      currentPage: 1,
      totalDataNumber: 0,
      editMachineInfo: "",
      brandList: "",
      storeList: "",
      areaList: "",
      oneTypeList: "",
      towTypeList: "",
      styleList: "",
      secondBrand: "",
      clevice_id: "",
      getBrandForm: {
        category_id: 19,
        org: 1,
      },
      formInline: {
        name: "",
        area_id: "",
        type_id: "",
        machine_id: "",
        category_id: 19,
      },
      saveMachineForm: {
        id: "",
        brand_id: "",
        store_id: "",
        area_id: "",
        oneTypeVal: "",
        type_id: "",
        style_id: "",
        no: "",
        trans_id: "",
        is_unitsshipped: "",
        category_id: "",
      },
      saveForm: {
        machine_id: "",
        coins_sell_vip: "",
        coins_sell: "",
        pulse: "",
        remark: "",
        is_gift_mapping: "",
        stars: "",
      },
      newForm: {},
      rules: {
        brand_id: [
          { required: true, message: "请选择品牌", trigger: "change" },
        ],
        store_id: [
          { required: true, message: "请选择门店", trigger: "change" },
        ],
        area_id: [{ required: true, message: "请选择区域", trigger: "change" }],
        oneTypeVal: [
          { required: true, message: "请选择一级分类", trigger: "change" },
        ],
        type_id: [
          { required: true, message: "请选择二级分类", trigger: "change" },
        ],
        no: [{ required: true, message: "请输入机台编号", trigger: "blur" }],
        coins_sell_vip: [
          { required: true, trigger: "blur", validator: validateVal },
        ],
        coins_sell: [
          { required: true, trigger: "blur", validator: validateVal },
        ],
        pulse: [{ required: true, trigger: "blur", validator: validateVal }],
        type: [{ required: true, message: "选择卡头类型", trigger: "change" }],
        stars: [{ required: false, trigger: "blur", validator: validateVal2 }],
      },
      addEditDialog: false,
      innerVisible: false,
      addCleviceDialog: false,
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.list) {
        // vm.fetchData();
        vm.getBrandList();
        vm.getStoreF(); //搜索条件的门店
        vm.getTypeF(); //搜索条件的类型
      }
    });
  },

  methods: {
    getStoreF() {
      getStore({ category_id: 19 }).then((response) => {
        this.store_list = response;
        //  console.log(response)
        let data = response.filter(
          (res) => res.id == this.$store.getters.optionId
        );
        if (data.length > 0) {
          this.formInline.name = data[0].id;
        } else {
          if (this.store_list.length > 0) {
            this.formInline.name = this.store_list[0].id;
          }
        }
        // console.log(this.formInline.name)
        this.storeChangeF();
      });
    },
    storeChangeF() {
      getArea({ sid: this.formInline.name }).then((response) => {
        response.unshift({ id: "", name: "全部区域" });
        this.area_list = response;
        this.formInline.area_id = response[0].id;
        if (this.list == null) {
          this.searchClick();
        }
      });
    },
    getTypeF() {
      getType({ id: -1 }).then((response) => {
        response.unshift({ id: "", name: "全部类型" });
        this.type_list = response;
      });
    },
    sweepChange() {
      if (!this.is_sweep) {
        this.is_mida = false;
        this.scanForm.rules = [];
        return;
      }
    },
    midaChange() {
      this.scanForm.rules = [];
      if (this.is_mida) {
        this.scanForm.combo_id = "";
        this.scanForm.rules = this.mida_list;
        return;
      }
      this.mealChange();
    },
    mealChange() {
      let item_list =
        this.scanForm.combo_id == ""
          ? []
          : this.package_list.filter((item, i) => {
              return item.id == this.scanForm.combo_id;
            })[0].rules;
      item_list.forEach((val) => {
        val.is_give = val.times_first * 1 > val.times * 1 ? true : false;
      });
      this.scanForm.rules = item_list;
      if (this.is_mida && this.scanForm.combo_id == "") {
        this.scanForm.rules = this.mida_list;
      }
    },
    scanSetClick(row, edit) {
      this.scanForm.id = row.id;
      this.scanForm.rules = [];
      this.package_list = [];
      this.package_info = row;
      this.is_sweep = row.is_sweep == 1 ? true : false;
      this.is_mida = row.combo_id == 6 ? true : false;
      this.scanForm.combo_id = row.combo_id == 6 ? "" : row.combo_id;
      get_package({ all: 1, brand_id: row.brand_id }).then((response) => {
        this.package_list = response.filter((item, i) => {
          return item.id != 6;
        });
        this.mealChange();
      });
      this.scanSetDialog = true;
    },
    // 提交
    saveClick(formName, addClevice) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveMachineForm.trans_id = this.isSwingCard == true ? 1 : 0;
          this.saveMachineForm.is_unitsshipped =
            this.isDelivery == true ? 1 : 0;
          save(this.saveMachineForm, this.url).then((response) => {
            if (response.code == 1) {
              this.machineInfo.machine_id = response.id;
              let obj = {};
              obj = this.storeList.find((item) => {
                return item.id === this.saveMachineForm.store_id;
              });
              this.machineInfo.store_name = obj.name;
              obj = this.areaList.find((item) => {
                return item.id === this.saveMachineForm.area_id;
              });
              this.machineInfo.area_name = obj.name;
              obj = this.towTypeList.find((item) => {
                return item.id === this.saveMachineForm.type_id;
              });
              this.machineInfo.machine_type = obj.name;
              obj = this.brandList.find((item) => {
                return item.id === this.saveMachineForm.brand_id;
              });
              this.machineInfo.brand_name = obj.name;
              this.machineInfo.no = this.saveMachineForm.no;
              this.machineInfo.brand_id = this.saveMachineForm.brand_id;
              this.machineInfo.id = response.id;
              if (this.editIndex || this.editIndex === 0) {
                this.list[this.editIndex].no = this.saveMachineForm.no;
              }
              this.addEditDialog = false;
              if (addClevice) {
                this.addCleviceDialogShow();
              }
              this.$message({
                type: "success",
                message: response.message,
              });
            }
          });
        }
      });
    },
    // 编辑和新增方法
    addEditClick(id, index) {
      this.editIndex = id ? index : "";
      this.dialogTitle = id ? "编辑机台信息" : "新增机台";
      this.url = id ? "/machine/base_arg" : "/Machine/create";
      this.isDisabled = id ? true : false;
      this.saveMachineForm.id = id ? id : "";
      this.saveMachineForm.category_id = id ? 51 : 50;
      this.editMachineInfo = "";
      this.towTypeList = "";
      getBrand({ org: 1, category_id: this.saveMachineForm.category_id }).then(
        (response) => {
          if (response[0].name === "子品牌") {
            response.splice(0, 1);
          }
          this.brandList = response;
          //this.brandList.splice(0, 1);
          //console.log(response)
        }
      );
      this.brandChange();
      getType({}).then((response) => {
        this.oneTypeList = response;
      });
      getStyle({
        brand: this.saveMachineForm.brand_id,
        category_id: this.saveMachineForm.category_id,
      }).then((response) => {
        this.styleList = response;
      });
      if (id) {
        this.dialogLoading = true;
        getEditMachine({ id: id, store_id: this.$route.params.store_id }).then(
          (response) => {
            this.editMachineInfo = response;
            this.saveMachineForm.store_id = this.editMachineInfo.store_id;
            this.saveMachineForm.brand_id = this.editMachineInfo.brand_id;
            this.saveMachineForm.oneTypeVal = this.editMachineInfo.first_type_id;
            this.storeChange();
            this.oneTypeChange();
            this.saveMachineForm.style_id = this.editMachineInfo.style_id;
            this.saveMachineForm.no = this.editMachineInfo.no;
            this.isSwingCard =
              this.editMachineInfo.trans_id * 1 > 0 ? true : false;
            this.isDelivery =
              this.editMachineInfo.is_unitsshipped == 0 ? false : true;
            this.dialogLoading = false;
          }
        );
      }
      this.addEditDialog = true;
    },
    getBrandList() {
      getBrand({ org: 1, category_id: 19 }).then((response) => {
        this.brandList = response;
      });
    },
    brandChange() {
      this.saveMachineForm.store_id = "";
      this.saveMachineForm.area_id = "";
      getStore({ brand: this.saveMachineForm.brand_id, category_id: 19 }).then(
        (response) => {
          if (response[0].name === "全门店") {
            response.splice(0, 1);
          }
          this.storeList = response;
          //this.storeList.splice(0, 1);
          this.storeChange();
        }
      );
    },
    storeChange() {
      this.saveMachineForm.area_id = "";
      getArea({ sid: this.saveMachineForm.store_id, category_id: 19 })
        .then((response) => {
          this.areaList = response;
          this.saveMachineForm.area_id = this.editMachineInfo.area_id;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    oneTypeChange() {
      this.saveMachineForm.type_id = "";
      getType({ id: this.saveMachineForm.oneTypeVal })
        .then((response) => {
          this.saveMachineForm.type_id = this.editMachineInfo.type_id;
          this.towTypeList = response;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    areaChange() {
      if (this.saveMachineForm.type_id && this.saveMachineForm.area_id) {
        this.getNo();
      }
    },
    towTypeChange() {
      if (this.saveMachineForm.type_id && this.saveMachineForm.area_id) {
        this.getNo();
      }
    },
    getNo() {
      get_area_no({
        type_id: this.saveMachineForm.type_id,
        area_id: this.saveMachineForm.area_id,
      }).then((response) => {
        let max_no =
          Math.max.apply(null, response) < 0
            ? 0
            : Math.max.apply(null, response);
        this.use_no_list = response;
        if (response.length == max_no) {
          this.default_no = `当前建议填写编号${max_no + 1}`;
        } else {
          for (let i = 1; i < max_no + 1; i++) {
            if (this.use_no_list.indexOf(i + "") == -1) {
              this.default_no = `当前建议填写编号${i}`;
              break;
            }
          }
        }
      });
    },
    delClick(id, index) {
      this.$confirm("此操作删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        del(id).then((response) => {
          if (response.code == 1) {
            this.list.splice(index, 1);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        });
      });
    },
    // 保存并添加卡头 -start---------
    // 提交
    saveCleviceClick(formName, keep) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.newForm = JSON.parse(JSON.stringify(this.saveForm));
          this.newForm.is_gift_mapping = this.isGiftMapping == true ? 1 : 0;
          this.newForm.combo_id = this.scanForm.combo_id;
          this.newForm.is_sweep = this.is_sweep ? 1 : 0;
          if (this.is_mida) {
            this.newForm.combo_id = "6";
          }
          if (this.is_sweep && !this.is_mida && this.scanForm.combo_id == "") {
            this.$message({ type: "error", message: "请选择套餐" });
            return;
          }
          if (this.clevice_id) {
            this.newForm.clevice_id = this.clevice_id;
          }
          saveClevice(this.newForm, this.url).then((response) => {
            if (response.code == 1) {
              this.url = "/Machine_clevice/create_machine_clevice";
              this.addCleviceDialog = keep ? true : false;
              this.saveForm.remark = "";
              this.$refs["saveForm"].resetFields();
              this.$message({ type: "success", message: response.message });
              this.scanSetClick({
                id: this.machineInfo.id,
                is_sweep: "",
                combo_id: "",
                brand_id: this.machineInfo.brand_id,
              });
            }
          });
        }
      });
    },
    // 卡头编辑和新增弹框
    addCleviceDialogShow(id, index) {
      this.scanSetClick({
        id: this.machineInfo.id,
        is_sweep: "",
        combo_id: "",
        brand_id: this.machineInfo.brand_id,
      });
      this.editIndex = "";
      this.isAdd = true;
      this.addCleviceDialog = true;
      this.url = "/Machine_clevice/create_machine_clevice"; //新建URL
      this.saveForm = {
        machine_id: this.machineInfo.machine_id,
        coins_sell_vip: "",
        coins_sell: "",
        type: "",
        pulse: "",
        remark: "",
        is_gift_mapping: "",
      };
    },

    // 新建卡头
    addCleviceClick() {
      this.$refs["saveForm"].resetFields();
      this.isDisabled = false;
      this.url = "/Machine_clevice/create_machine_clevice";
      this.clevice_id = "";
      this.saveForm.remark = "";
      this.isGiftMapping = false;
      this.scanSetClick({
        id: this.machineInfo.id,
        is_sweep: "",
        combo_id: "",
        brand_id: this.machineInfo.brand_id,
      });
      // console.log(this.saveForm)
    },
    // 卡头搜索
    searchCleviceClick() {
      getClevice({ clevice_id: this.clevice_id }).then((response) => {
        this.$refs["saveForm"].resetFields();
        if (response.list.length > 0) {
          this.url = "/Machine_clevice/machine_clevice_mapping"; //添加搜索出来的URL
          this.searchCleviceInfo = response.list[0];
          this.saveForm.coins_sell_vip = this.searchCleviceInfo.coins_sell_vip;
          this.saveForm.coins_sell = this.searchCleviceInfo.coins_sell;
          this.saveForm.pulse = this.searchCleviceInfo.pulse;
          this.saveForm.remark = this.searchCleviceInfo.remark;
          this.saveForm.clevice_no = this.searchCleviceInfo.clevice_no
            ? this.searchCleviceInfo.clevice_no
            : "-"; //后端不在 卡头编号不需要，临时改传-，传0或不传都不行，我擦？
          this.saveForm.type = this.searchCleviceInfo.type;
          this.saveForm.stars =
            this.searchCleviceInfo.stars == 0
              ? ""
              : this.searchCleviceInfo.stars;
          this.isGiftMapping =
            this.searchCleviceInfo.is_gift_mapping == 1 ? true : false;
          this.isDisabled = true;
          this.scanSetClick({
            id: this.machineInfo.id,
            is_sweep: this.searchCleviceInfo.is_sweep,
            combo_id: this.searchCleviceInfo.combo_id,
            brand_id: this.machineInfo.brand_id,
          });
        } else {
          this.$refs["saveForm"].resetFields();
          this.isDisabled = false;
          this.clevice_id = "";
          this.saveForm = {
            machine_id: this.$route.params.machine_id,
            coins_sell_vip: "",
            type: "",
            coins_sell: "",
            pulse: "",
            remark: "",
            is_gift_mapping: "",
          };
          this.isGiftMapping = false;
          this.scanSetClick({
            id: this.machineInfo.id,
            is_sweep: "",
            combo_id: "",
            brand_id: this.machineInfo.brand_id,
          });
          this.$message({
            type: "error",
            message: "找不到该卡头，请重新输入",
          });
        }
      });
    },
    // 保存并添加卡头 -end---------
    // 搜索
    searchClick() {
      this.fetchData();
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then((response) => {
          this.totalDataNumber = response.total * 1;
          this.machine_num = response.total;
          getList(this.formInline).then((response) => {
            if (response.length > 0) {
              this.list = response;
              // console.log("查看",response)
              this.store_name = response[0].store_name;
            } else {
              this.list = [];
            }
            this.listLoading = false;
          });
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then((response) => {
        this.list = response;
        this.listLoading = false;
      });
    },
    // 表单认证
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //关闭弹框重置
    callOf(formName) {
      this.addEditDialog = false;
      this.saveMachineForm.style_id = "";
      this.isSwingCard = false;
      this.isDelivery = false;
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style>
.machineContainer .el-dialog {
  width: 660px;
}
</style>